package com.esun.util.encryption;

import java.net.URLEncoder;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.util.HashMap;

import com.esun.util.StringUtil;
import com.esun.util.log.EDebug;

public class Encryption {
	
	private String strResult; // 
	
	private static final String N = "127229136530197319995824967680642139919235321062370946240649169968513580214039961222056295505935057826728768685902472356590118370107335292018409043321909481365388870741185779074098959382117257066188651500217908313018531124891094389984145171642255665597934617509986877167115705425381951138191902941098813458581";
	private static final String D = "75422725839368541116587512692992168973285494584649490095601887520739751665405290651664084970338632095247560801013121649234519260830371903355466526421708414845304904090920045874826602802511974315761510049768490171212567497292613723660488002453436631712083516317761509458552484517117419111964921338576542504129";
	private static final String E = "65537";
	
	private static final int RSALIMITED = 38; // rsa解析长度限制
	
	PublicKey pK; // 公钥
	PrivateKey prK; // 私钥
	
	
	private void setStrResult(String strResult) {
		this.strResult = strResult;
	}
	
	public String doEncryption (String resultNoRSA) {
		
		String encrypStr = "" ;
		
		try {
			pK = RSA.getPublicKey(N, E);
			prK = RSA.getPrivateKey(N, D);
		} catch (Exception e) {
			EDebug.o(this, "公钥，密钥获取失败");
		}
		
		if (resultNoRSA.length() < RSALIMITED + 1) {
			
			encrypStr = StringUtil.comJson(getStringDoRSAAndBase64(resultNoRSA)) ;
			
		} else {
			encrypStr = StringUtil.comJson(splitStingForRSA(resultNoRSA));
		}
		
		
		EDebug.o("最终json串========================》" + encrypStr) ;
		
		return encrypStr ;
		
	}
	
	/**
	 * @Title: getListDoRSAAndBase64
	 * @Description: (对构建好的xml文件进行RSA加密以及Base64编码处理)
	 * @param resultNoRSA
	 * @param @return params
	 * @return String 返回类型
	 * @throws
	 */
	private HashMap<String, String> getStringDoRSAAndBase64(String resultNoRSA) {
		
		HashMap<String, String> jsonMap = new HashMap<String, String>() ;
		
		byte[] byteForRSA;
		
		EDebug.o(this, "postdata总长少于100");
		byteForRSA = resultNoRSA.getBytes();
		// 对byte[]进行RSA加密
		byteForRSA = RSA.encrypt((RSAPublicKey) pK, byteForRSA);
		EDebug.o(this, "RSA密文# " + StringUtil.bytesToString(byteForRSA));

		// 对RSA密文进行Base64编码
		setStrResult(Base64.base64Encoder(byteForRSA));
		EDebug.o(this, "base64密文# " + strResult);
		
		jsonMap.put("msgcount", "1") ;
		jsonMap.put("msg0", URLEncoder.encode(strResult)) ;

		return jsonMap ;

	}

	/**
	 * @Title: splitStingForRSA
	 * @Description: (对超过RSA加密长度限制的String拆分并进行RSA&Base64操作)
	 * @param strData
	 *            String 需要拆分的xml
	 * @param RSALIMITED
	 *            int 拆分的长度
	 * @param @return list
	 * @return String 返回类型
	 * @throws
	 */
	private HashMap<String, String> splitStingForRSA(String strData) {
		
		HashMap<String, String> jsonMap = new HashMap<String, String>() ;

		int countMsg;
		if (strData.length() % RSALIMITED == 0) {
			countMsg = strData.length() / RSALIMITED;
		} else {
			countMsg = strData.length() / RSALIMITED + 1;
		}
		
		jsonMap.put("msgcount", URLEncoder.encode(Integer.toString(countMsg))) ;
		
		for (int i = 0; i < countMsg; i++) {
			byte[] byteStrData;
			String strItem;
			EDebug.o(this, "第" + Integer.toString(i + 1) + "条msg,总" + Integer.toString(countMsg) + "条");
			if (i != countMsg - 1) {
				strItem = strData.substring(i * RSALIMITED, ((i + 1) * RSALIMITED));
				EDebug.o(this, "拆分无加密# " + strItem);
			} else {
				strItem = strData.substring(i * RSALIMITED, strData.length());
				EDebug.o(this, "拆分无加密# " + strItem);
			}
			byteStrData = strItem.getBytes();
			byteStrData = RSA.encrypt((RSAPublicKey) pK, byteStrData);
			EDebug.o(this, "拆分后RSA密文# " + StringUtil.bytesToString(byteStrData));
			setStrResult(Base64.base64Encoder(byteStrData));
			EDebug.o(this, "拆分后base64密文# " + strResult);
			String msgCount = "msg" + Integer.toString(i);
			
			jsonMap.put(msgCount, URLEncoder.encode(strResult)) ;
			
			EDebug.o(this, "msgcount=" + msgCount + "|| values=" + strResult);

		}
		return jsonMap ;
	}
	
	/**
	 * @Title: getListNoRSAButBase64
	 * @Description: (对构建好的xml文件进行Base64编码处理,不进行RSA加密)
	 * @param resultNoRSA
	 * @param @return params
	 * @return String 返回类型
	 * @throws
	 */
	public String getListNoRSAButBase64(String resultNoRSA) {

		byte[] byteForBase64;
		byteForBase64 = resultNoRSA.getBytes();
		setStrResult(Base64.base64Encoder(byteForBase64));
		EDebug.o(this, "非加密状态base64编码# " + strResult);
		
		return URLEncoder.encode(strResult);

	}

}
